package com.lizhen.core.datastructure;


import java.io.*;


public class SerializableDataStructure {

    private static final String FILE_PATH = "1.8\\api\\main\\resource\\com\\lizhen\\core\\datastructure\\SerializableDataStructure\\serializable.ser";

    public SerializableDataStructure() {

    }

    public static void main(String[] args) {
        Employee e1 = new Employee();
        e1.name = "Reyan Ali";
        e1.address = "Phokka Kuan, Ambehta Peer";
        e1.SSN = 11122333;
        e1.number = 101;
        try {
            FileOutputStream fos1 = new FileOutputStream(FILE_PATH);
            ObjectOutputStream oos1 = new ObjectOutputStream(fos1);
            oos1.writeObject(e1);
            oos1.close();
            fos1.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Employee e2 = null;
        try {
            FileInputStream fis12 = new FileInputStream(FILE_PATH);
            ObjectInputStream ois12 = new ObjectInputStream(fis12);
            e2 = (Employee) ois12.readObject();
            ois12.close();
            fis12.close();
        } catch (IOException e) {
            e.printStackTrace();
            return;
        } catch (ClassNotFoundException e) {
            System.out.println("Employee class not found");

            e.printStackTrace();
            return;
        }
        System.out.println("Deserialized Employee...");
        System.out.println("Name: " + e2.name);
        System.out.println("Address: " + e2.address);
        System.out.println("SSN: " + e2.SSN);
        System.out.println("Number: " + e2.number);
    }

    static class Employee implements java.io.Serializable {
        public String name;
        public String address;
        public transient int SSN;
        public int number;

        public void mailCheck() {
            System.out.println("Mailing a check to " + name + " " + address);
        }
    }
}
